<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
class BusinessController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//商务列表
    public function index(){
        $search = I('search');
        if($search){
            $where['user_name|phone'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['business'];
        $where['type'] = 1;
        $count = M('business')->where($where)->count();
       


        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出


        $list = M('business')->where($where)->field('id,user_name,phone,email,qq,add_time,admin_id')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $k => $v) {
            $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
            $admin_id[] = $v['admin_id'];
            unset($list[$k]['admin_id']);
        }
        if($admin_id){
            $where['admin_id'] = array('not in',$admin_id);
        }
        $lists = M('admin')->where($where)->field('admin_id,user_name')->select();
        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Business');
        $this->assign("lists", $lists);
        $this->display();
    }


     public function edit(){
        $id = I('id');
        if(!$id){
            $this->ajaxreturn(array('state' => 0, 'data' => '信息错误'), 'json');
        }
        $where['id'] = $id;
        
        $list = M('business')->where($where)->field('id,admin_id,user_name,phone,email,qq')->find();
        if(empty($list)){
            $this->ajaxreturn(array('state' => 0, 'data' => '不存在该用户'), 'json');
        }else{
            $value = $list['admin_id'];
            unset($list['admin_id']);
            $this->ajaxreturn(array('state' => 1, 'data' => $list,'value'=>$value), 'json');
        }
    }


    //商务编辑
    public function handle(){
        $res = I('');
        if($res['act'] == 'add'){
           
            if(!$res['user_name'] ||  !$res['phone'] || !$res['qq'] || !$res['email'] || !$res['admin_id'] || !$res['password']){
                $this->error('请填写完整信息');
            }
           
            $res['add_time'] = time();
            $res['update_time'] = time();
            $res['type'] = 1;
            $res['password'] = md5($res['password']);
            $list = M('business')->add($res);
            if($list){
                $this->success("添加成功", U("Business/index"));exit;
            }else{
                $this->error("添加失败");exit;
            }

            $this->display();
        }elseif($res['act'] == 'edit'){
            $data = [];
            $update['user_name'] = $res['user_name'];
            $update['phone'] = $res['phone'];
            $update['email'] = $res['email'];
            $update['admin_id'] = $res['admin_id'];
            $update['qq'] = $res['qq'];
            $update['update_time'] = time();
            $where['id'] = $res['id'];
            $list = M('business')->where($where)->save($update);
            if($list){
                $this->success("更新成功", U("Business/index"));exit;
            }else{
                $this->error("更新失败");exit;
            }
        }elseif($res['act'] == 'del'){
            $where['id'] = $res['id'];
            $list = M('business')->where($where)->delete($res);
            if($list){
                $this->success("删除成功", U("Business/index"));exit;
            }else{
                $this->error("删除失败");exit;
            }
        }
       
        
    }




    //官网推首页
    public function ptindex(){
        layout('Layout/twolayout');
        $this->display();
    }

    public function pttable(){
        // $header = ['用户名','公司名称','电话','邮箱','更新时间'];
        $header = C('header')['BusinessList'];
        $where['type'] = 1;
        $list = M('admin')->where($where)->field('user_name,company_name,phone,email,add_time')->select();
        foreach ($list as $k => $v) {
            $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }


    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $files[$file['key']] =  '/Uploads/'.$file['savepath'].$file['savename'];
            }
        }

        return $files;
    }
}